package lu.med.praxiswahl.wahlumed.client.arztdatabase;

import java.util.ArrayList;
import java.util.List;
import lu.med.praxiswahl.wahlumed.client.utilities.Parser;
import lu.med.praxiswahl.wahlumed.client.wclient.Main;
import lu.med.praxiswahl.wahlumed.model.beans.DoctorsTBL;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.DoctorServiceInt;

// Klasse zur leichteren Verwaltung der �rzte Datenbank

/**
 * 
 * @author Lars Holger Roth
 */
public class ArztDatabase {
	private ArrayList<ArztTableDataset> data = new ArrayList<ArztTableDataset>();

	private Main dataManager;

	public ArztDatabase(Main dataManager) {
		this.dataManager = dataManager;
		// refresh();
	}

	public ArztTableDataset getDataset(int index) {
		return data.get(index);
	}

	public String getEntry(String key) {
		String[] tok = Parser.parseString(key, '_');
		for (int i = 0; i < data.size(); i++) {
			if ((data.get(i).getBufferAt(0).equals(tok[0]))
					&& (data.get(i).getBufferAt(1).equals(tok[1]))) {
				ArztTableDataset dr = data.get(i);
				String ret = dr.getBufferAt(0) + "-" + dr.getBufferAt(1) + "; "
						+ dr.getBufferAt(2) + "; " + dr.getBufferAt(4);
				return ret;
			}
		}
		return "N/A";
	}

	public String getEntry(String name, String vorname) {
		// Liefert anhand Name und Vorname den ersten pasenden Arzt
		for (int i = 0; i < data.size(); i++) {
			if ((data.get(i).getBufferAt(0).equals(name))
					&& (data.get(i).getBufferAt(1).equals(vorname)))
				return data.get(i).toString();
		}
		return "N/A";
	}

	public String getEntryText(int index) {
		ArztTableDataset dr = data.get(index);
		String ret = dr.getBufferAt(0) + "-" + dr.getBufferAt(1) + "; "
				+ dr.getBufferAt(2) + "; " + dr.getBufferAt(4);
		return ret;
	}

	public String getFullEntryText(int index) {
		ArztTableDataset dr = data.get(index);
		String ret = dr.getBufferAt(0) + " (" + dr.getBufferAt(3) + ") -"
				+ dr.getBufferAt(1) + "; " + dr.getBufferAt(2) + "; Tel. "
				+ dr.getBufferAt(4) + "; Fax " + dr.getBufferAt(5)
				+ "; E-Mail " + dr.getBufferAt(6);
		return ret;
	}

	public int getSize() {
		return data.size();
	}


	public void refresh() {

		// Holt sich die Tabelle neu vom Server
		data.clear();

		// Holt sich alle Einträge der Ärzte Tabelle von der Datenbank
		// ContextFactoryRemoteInt remote_context = new ContextFactoryRemote();
		// DoctorServiceInt doc =
		// remote_context.getRemoteContextDoctorService();
		DoctorServiceInt doc = this.dataManager.getDoctorContext();

		// Liste mit IDs abrufen
		List<DoctorsTBL> tmpDoctors = doc.listDoctorsItems();

		for (int i = 0; i < tmpDoctors.size(); i++) {
			ArztTableDataset buffer = new ArztTableDataset();

			/*
			 * F�r die Bedeutung dieser Zeile bitte in DoctorServiceInt
			 * nachlesen
			 */

			buffer.setBufferAt(tmpDoctors.get(i).getNachname(), 0);
			buffer.setBufferAt(tmpDoctors.get(i).getVorname(), 1);
			buffer.setBufferAt(tmpDoctors.get(i).getAdresse(), 2);
			buffer.setBufferAt(tmpDoctors.get(i).getFachgebiet(), 3);
			buffer.setBufferAt(tmpDoctors.get(i).getTelefon(), 4);
			buffer.setBufferAt(tmpDoctors.get(i).getFax(), 5);
			buffer.setBufferAt(tmpDoctors.get(i).getEmail(), 6);
			buffer.setID(tmpDoctors.get(i).getId());
			data.add(buffer);
		}
	}
}
